package com.toyi.util;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
 * This class defines a set of properties in a
 * key-value pair relationship.
 */
public class Configuration extends Properties
{
	//---------------------------------------------------------------------------------------------
	// Class Variables
	//---------------------------------------------------------------------------------------------
	private static final String PROPERTY_FILE_SEP_KEY = "file.separator";

	//---------------------------------------------------------------------------------------------
	// Constructors
	//---------------------------------------------------------------------------------------------
	/**
	 * Creates a Configuration object by loading
	 * the key-value pairs found in the property file.
	 * Each key-value pair is separated by a <B>=</B>.
	 * @param configFile the property file.
	 * @throws FileNotFoundException if the property file does not exist.
	 * @throws IOException if any error occurred while reading the property file.
	 */
	public Configuration(String configFile)
			throws IOException, FileNotFoundException
	{
		InputStream in = null;
        try{
            try {
				in = new FileInputStream(configFile);
			} catch (FileNotFoundException e) {
				in = this.getClass().getResourceAsStream(configFile);
			}
        	
            if(in != null){
                load(in);                
            }
        }
        finally{
            if(in != null){
                try{
                    in.close();
                }catch(Exception e){}
            }
        }

	}

	/**
	 * Creates a Configuration object by loading
	 * the key-value pairs found in the property file.
	 * Each key-value pair is separated by a <B>=</B>.
	 * @param configPath the property file path less trailing separator.
	 * @param configType the property filename.
	 * @param suffix the property file's suffix.
	 * @throws FileNotFoundException if the property file does not exist.
	 * @throws IOException if any error occurred while reading the property file.
	 */
	public Configuration(String configType, String suffix)
			throws IOException, FileNotFoundException
	{
		String configFile = configType + suffix;
		InputStream in = null;
        try{
            try {
				in = new FileInputStream(configFile);
			} catch (FileNotFoundException e) {
				configFile = configType + suffix;
				in = this.getClass().getResourceAsStream(configFile);
			}

            if(in != null){
                load(in); 
            }
        }
        finally{
            if(in != null){
                try{
                    in.close();
                }catch(Exception e){}
            }
        }        

	}
}
